package com.jsoup.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsoup.bean.Article;
import com.jsoup.service.ArticleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/articleManage")
public class ArticleManageController {

    @Resource
    private ArticleService articleService;

    @RequestMapping(value = "/addArticle", method = RequestMethod.GET)
    @RequiresRoles("admin")
    public String addArticle() {
        return "manage-article-add";
    }

    /**
     * 更新博文
     *
     * @param article
     * @return
     */
    @RequestMapping("/updateArticle")
    @RequiresRoles("admin")
    @ResponseBody
    public Object updateArticle(Article article) {
        Map<Object, Object> map = new HashMap<>();
        int count = articleService.updateByPrimaryKeySelective(article);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 获得博文
     * 返回更新页
     *
     * @param articleId
     * @param model
     * @return
     */
    @RequestMapping("/getArticle")
    @RequiresRoles("admin")
    public String getArticle(@RequestParam(value = "articleId") Integer articleId, Model model) {
        Article article = new Article();
        article.setArticleId(articleId);
        Article art = articleService.getArticle(article);
        model.addAttribute("article", art);
        return "manage-article-update";
    }

    /**
     * 模糊搜索博文
     *
     * @param pn
     * @param content
     * @param model
     * @return
     */
    @RequestMapping("/searchArticle")
    public String searchArticle(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                @RequestParam(value = "content") String content,
                                Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        Article article = new Article();
        article.setArticleTitle(content);
        PageHelper.startPage(pageNo, 10);
        List<Article> articleList = articleService.searchArticle(article);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-article";
    }

    /**
     * 发表博文
     *
     * @param article
     * @return
     */
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public Object addArticle(Article article) {
        Map<Object, Object> map = new HashMap<>();
        article.setArticleViews(0);
        article.setArticleCreateDate(new Timestamp(System.currentTimeMillis()));
        int count = articleService.insertSelective(article);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 删除博文
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/deleteArticle")
    @RequiresRoles("admin")
    @ResponseBody
    public Object deleteArticle(@RequestParam(value = "articleId") Integer articleId) {
        Map<Object, Object> map = new HashMap<>();
        int count = articleService.deleteByPrimaryKey(articleId);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 文章列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/articleList")
    @RequiresRoles("admin")
    public String articleList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        List<Article> articleList = articleService.selectArticle(new Article());
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-article";
    }
}
